import '../app/quadcoptershared.dart';
//import '../app/quadcopterclient.dart';
import 'dart:async';
import 'dart:math' as math;


math.Random RND = new math.Random();
Duration PERIOD = new Duration(milliseconds:2100);

// copter that flies in a circle
class TestCopter extends QuadCopter {
  
  DateTime createTime = new DateTime.now( );
  double radius;
  double startAngle;
  int direction = 1;
  TestCopter(String name, double radius) : super(name) {
     this.radius = radius;
  }
  
  // just reverse
  void setVelocity(Velocity v) {
   startAngle =  getAngle();
    direction = -direction;
  }

  num getAngle() {
    DateTime current = new DateTime.now( );
    Duration del = current.difference(lastClick);
    double msec = del.inMilliseconds.toDouble();
    return direction * math.PI * 2  * msec / 20000;
  }
  
  Point position()
  {
    
    num angle =  getAngle();
    angle += angle;
    
    num newX =  radius * math.sin(angle);
    num newY =  radius * math.cos(angle);
    
     return new Point(newX,newY);
    
  }
  
}

class TestUInterface { // extends UIInterface {
  
  String _myID;
  var velocityChangeListener;
  QuadCopterManager manager;
  Timer shower;
  Timer reverser;
  
  
// pass in the id for the copter which is Me
// this will be called just once
// called once with e function taking a Velocity object and returning void
// call this when the user changes velocity
    void initialize(String id,void f( Velocity value)) {
      _myID  = id;
      velocityChangeListener =  f;
      
    }
//
// pass in the lasted set of copters 
// each copter has function position() returning a Point 
// where x and y are -1000 ... 1000
// the function returns the position at the current time 
    void updateCopters(QuadCopterManager latest) { 
      // first update
      if(manager == null) {
        shower = new Timer.periodic(PERIOD, onUpdateCopters);
        shower = new Timer.periodic(PERIOD * 5, maybeReverseCopters);
      }
       manager = latest;
    }
    
    void onUpdateCopters(Timer ignored)
    {
      manager.forEach(showCopter);
       
    }
    
    void maybeReverseCopters(Timer ignored)
    {
      manager.forEach(maybeReverse);
       
    }

    void maybeReverse(QuadCopter copter)
    {
          if(RND.nextDouble() < 0.5) {
            print( "reversing " + copter.name);
            copter.setVelocity(null);
          }
    }

    void showCopter(QuadCopter copter)
    {
       Point pos = copter.position();
       print(copter.name + " " + pos.toString() );
    }

}

void doNothing(Velocity v) {
  print( "nothing done\n");
}


// All my test code there may be a better way 
LocalGame buildLocalGame() {
  
  LocalGame ret = new LocalGame();
  
  
   // make a few copters 
  QuadCopter joe = new TestCopter("joe",500.0 );
  ret.myName = "joe";
  ret.myCopterId = joe.id;
  
  QuadCopter steve = new TestCopter("steve",400.0);
  
  QuadCopterManager mgr = new QuadCopterManager();
  mgr.addCopter(joe);
  mgr.addCopter(steve);
 
 ret.manager = mgr;
 
 TestUInterface ui = new TestUInterface();
 ui.initialize(joe.id, doNothing);
 ui.updateCopters(mgr);
   
}


void playGame() {
  
}


/**
 * For non-trivial uses of the Chrome apps API, please see the 'chrome'
 * package.
 * 
 * http://pub.dartlang.org/packages/chrome
 * http://developer.chrome.com/apps/api_index.html
 */
void main() {
  buildLocalGame();
}  

 